<!--********************************************************************
* Copyright© 2000 - 2020 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入 
* Echarts (https://github.com/apache/echarts)
* vue-echarts (https://github.com/ecomfe/vue-echarts)
* vue-iclient (https://github.com/SuperMap/vue-iclient)
* mapbox-gl-enhance (https://iclient.supermap.io/web/libs/mapbox-gl-js-enhance/1.12.1-12/mapbox-gl-enhance.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title data-i18n="resources.title_componentsTimeLineSupermap_Vue"></title>
        <script type="text/javascript" include="vue,jquery" src="../js/include-web.js"></script>
        <script
            include="echarts-vue,iclient-mapboxgl-vue,mapbox-gl-enhance"
            src="../../dist/mapboxgl/include-mapboxgl.js"
        ></script>
        <style>
            #main {
                margin: 0 auto;
                width: 100%;
                height: 100%;
            }
            .sm-component-time-line {
                position: absolute;
                bottom: 20px;
                width: 100%;
                height: 80px;
                z-index: 1000;
            }
            .sm-supermap-title {
                position: absolute;
                top: 20px;
                left: 20px;
                width: 600px;
                height: 50px;
                z-index: 1000;
            }
            .sm-time-info {
                position: absolute;
                top: 20px;
                right: 50px;
                width: 300px;
                height: 50px;
                z-index: 1000;
            }
            .sm-component-spin {
                background: transparent !important;
            }
        </style>
    </head>

    <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
        <div id="main">
            <sm-web-map
                server-url="https://www.supermapol.com"
                :map-id="35063750"
                :tianditu-key="tiandituKey"
                :map-options="mapOptions"
                :loading="loading"
                @load="load"
            ></sm-web-map>
            <sm-time-line
                ref="timeLine"
                :data="data"
                :play-interval="2000"
                :next-enable="nextEnable"
                :label="label"
                @timelinechanged="timelineChanged"
                @timelineplaychanged="timelineplaychanged"
            ></sm-time-line>

            <sm-border type="border6" class="sm-supermap-title">
                <sm-text
                    title="超图大厦（成都）周边历史影像"
                    :font-style="{fontSize: '28px', fontWeight: 600}"
                ></sm-text>
            </sm-border>
            <sm-border type="border6" class="sm-time-info">
                <sm-text :title="timeInfo" :font-style="{fontSize: '18px', fontWeight: 600}"></sm-text>
            </sm-border>
        </div>
        <script>
            var label = [
                '2016年11月26日',
                '2017年4月13日',
                '2017年5月25日',
                '2017年11月11日',
                '2017年12月19日',
                '2018年1月10日',
                '2018年5月25日',
                '2019年11月11日'
            ];
            new Vue({
                el: '#main',
                data() {
                    return {
                        mapStatus: [],
                        mapQueue: [],
                        loading: false,
                        readyNext: true,
                        nextEnable: false,
                        tiandituKey: 'f16b023603de8ae8fdd09a2c0feb1ec2',
                        tooltip: {
                            formatter: function(params) {
                                return params.name;
                            }
                        },
                        label: {
                            formatter: function(val, index) {
                                return label[index];
                            }
                        },
                        url:
                            'https://www.supermapol.com/proxy/a9b367e2260de1e2/iserver/services/map_supermap_building1_rdef24g1/rest/maps/',
                        encodeUrl:
                            '/image.png?viewBounds=%7B"leftBottom"%3A%7B"x"%3A11586898.539611159%2C"y"%3A3570491.9618448047%7D%2C"rightTop"%3A%7B"x"%3A11588127.387288133%2C"y"%3A3571087.184938393%7D%7D%20&center=%7B"x"%3A11587505.74%2C"y"%3A3570771.31%7D&width=1920&scale=0.000346564548&prjCoordSys=%7B"epsgCode"%3A3857%7D&transparent=true&height=930',
                        data: [
                            'T20161126',
                            'T20170413',
                            'T20170525',
                            'T20171111',
                            'T20171219',
                            'T20180110',
                            'T20180525',
                            'T20191111'
                        ],
                        timeInfo: label[0],
                        mapOptions: {
                            style: { version: 8, sources: {}, layers: [] },
                            center: [104.0924, 30.5217],
                            zoom: 18,
                            bearing: 0,
                            pitch: 0,
                            tileSize: 1024,
                            rasterTileSize: 1024
                        }
                    };
                },
                computed: {
                    allLoaded() {
                        if (this.mapStatus.length) {
                            var allLoaded = true;
                            this.mapStatus.forEach(function(item) {
                                if (!item.status) {
                                    allLoaded = false;
                                }
                            });
                            return allLoaded;
                        }
                        return false;
                    }
                },
                watch: {
                    mapStatus: {
                        handler() {
                            var nextEnable = false;
                            var currentIndex = this.currentIndex;
                            this.mapStatus.forEach(function(item, index) {
                                if (currentIndex === index && item.status) {
                                    nextEnable = true;
                                }
                            });
                            this.nextEnable = nextEnable;
                        }
                    },
                    allLoaded() {
                        this.playState && this.setPlayState(true);
                        this.nextEnable = null;
                    }
                },
                created() {
                    SuperMap.Components.setTheme({ textColor: '#fff', background: 'rgb(0,0,0,0)' });
                    this.$on('addlayerssucceeded', this.updateNextRasterSource);
                    this.$on('loadingChange', this.loadingChange);
                },
                methods: {
                    load(e) {
                        this.map = e.map;
                        this.resetMapLoadStatus();
                    },
                    loadingChange(status) {
                        this.loading = status;
                    },
                    timelineplaychanged(val) {
                        this.playState = val.playState;
                    },
                    timelineChanged(val) {
                        var currentIndex = (val && val.currentIndex) || 0;
                        var dataId = this.data[currentIndex];
                        var imageUrl = this.url + dataId + this.encodeUrl;
                        this.currentIndex = currentIndex;
                        var sourceId = this.data[0];
                        if (!this.readyNext) {
                            this.mapQueue.push({ key: dataId, sourceId: sourceId, imageUrl: imageUrl });
                            return;
                        }
                        this.readyNext = false;
                        this.timeInfo = label[currentIndex];
                        this.updateRasterSource(sourceId, imageUrl);
                    },
                    updateRasterSource(sourceId, imageUrl) {
                        if (this.map.getLayer(sourceId)) {
                            this.map.setLayoutProperty(sourceId, 'visibility', 'none');
                        }
                        sourceId = sourceId + '_image';
                        var source = this.map.getSource(sourceId);
                        if (!source) {
                            this.map.addSource(sourceId, {
                                type: 'image',
                                url: imageUrl,
                                coordinates: [
                                    [104.08688053674865, 30.524020349913428],
                                    [104.09791946324947, 30.524020349913428],
                                    [104.09791946324947, 30.519414264971005],
                                    [104.08688053674865, 30.519414264971005]
                                ]
                            });
                            this.map.addLayer({
                                id: sourceId,
                                type: 'raster',
                                source: sourceId,
                                paint: {
                                    'raster-fade-duration': 0
                                }
                            });
                        } else {
                            source.updateImage({ url: imageUrl });
                        }
                        this.$emit('addlayerssucceeded');
                    },
                    updateNextRasterSource() {
                        this.readyNext = true;
                        if (this.mapQueue.length) {
                            var { id: mapId, sourceId, imageUrl } = this.mapQueue.shift();
                            this.updateRasterSource(sourceId, imageUrl);
                        } else {
                            var key = this.data[this.currentIndex];
                            var sourceCaches = this.map.style.sourceCaches;
                            var beforeIds = [];
                            for (var sourceId in sourceCaches) {
                                beforeIds.push(sourceId);
                            }
                        }
                        !this.allLoaded && this.isAllSourceLoaded(key, beforeIds);
                    },
                    setPlayState(status) {
                        this.$refs.timeLine.setPlayState(status);
                    },
                    isAllSourceLoaded(key, sourceIds) {
                        if (this.timer) {
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                        var map = this.map;
                        var _this = this;
                        this.timer = setInterval(function() {
                            _this.$emit('loadingChange', true);
                            var loaded = true;
                            sourceIds.forEach(function(id) {
                                if (_this.map && _this.map.style) {
                                    if (!_this.map.getSource(id)) {
                                        return true;
                                    }
                                    if (!_this.map.isSourceLoaded(id)) {
                                        loaded = false;
                                    }
                                    return _this.map.isSourceLoaded(id);
                                }
                                return true;
                            });
                            _this.nextEnable = false;
                            if (loaded && _this.mapStatus) {
                                clearInterval(_this.timer);
                                _this.timer = null;
                                var mapStatus = _this.mapStatus.concat();
                                mapStatus.forEach(function(info) {
                                    if (info.key === key) {
                                        info.status = true;
                                    }
                                });
                                _this.mapStatus = mapStatus;
                                _this.$emit('loadingChange', false);
                            }
                        }, 100);
                    },
                    resetMapLoadStatus() {
                        var mapStatus = [];
                        if (this.mapStatus && this.mapStatus.length) {
                            mapStatus = this.mapStatus.map(function(info) {
                                info.status = false;
                                return info;
                            });
                        } else {
                            this.data.forEach(function(key) {
                                mapStatus.push({ key: key, status: false });
                            });
                        }
                        this.mapStatus = mapStatus;
                    }
                }
            });
        </script>
    </body>
</html>
